/*!
    \copyright  Copyright (c) 2019 - 2023 Qualcomm Technologies International, Ltd.\n
                All Rights Reserved.\n
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file       
    \addtogroup connection_manager
    @{
    \brief      Header file for Connection Manager internal messages
*/

#ifndef __CON_MANAGER_MSG_H
#define __CON_MANAGER_MSG_H

#include <connection_manager.h>
#include "connection_manager_list.h"

/*! Connection Manager message IDs. */
typedef enum
{
        /*! Message sent to handler for a connection to update the QoS for
            that connection */
    CON_MANAGER_INTERNAL_MSG_UPDATE_QOS = INTERNAL_MESSAGE_BASE,
        /*! Message sent to the connection manager task to open an ACL.
            Internal message used to allow QoS to be configured before the
            connection is created */
    CON_MANAGER_INTERNAL_MSG_OPEN_TP_ACL,
        /*! Message sent to handle update of connection parameters once 
            GATT service discovery timer expires */
    CON_MANAGER_INTERNAL_MSG_TIMER_UPDATE_QOS,
        /*! Internal message used to just set the latest requested qos.
            Sent conditionally to avoid multiple qos in progress */
    CON_MANAGER_INTERNAL_MSG_APPLY_QOS,
        /*! Internal message used to reset the lock on updating qos.
            Added as failsafe mechanism. */
    CON_MANAGER_INTERNAL_MSG_QOS_TIMEOUT,

    /*! This must be the final message */
    CONN_MANAGER_INTERNAL_MESSAGE_END
} con_manager_internal_msg_id_t;
ASSERT_INTERNAL_MESSAGES_NOT_OVERFLOWED(CONN_MANAGER_INTERNAL_MESSAGE_END)

/*! \brief Structure of message used for opening of an ACL (using a typed address, tpaddr) */
typedef struct
{
    tp_bdaddr tpaddr;   /*!< Typed address to open */
} CON_MANAGER_INTERNAL_MSG_OPEN_TP_ACL_T;

/*! \brief Internal structure aggregating all information from an ACL-opened indication  */
typedef struct
{
    tp_bdaddr tpaddr;       /*!< Typed address of opened ACL */
    bool success;           /*!< ACL open succeeded? */
    hci_status status;      /*!< HCI status of the connection attempt */
    uint16 flags;           /*!< DM flags, meanings defined in dm_prim.h */
    bool incoming;          /*!< ACL is incoming? */
    uint16 interval;        /*!< LE Connection Interval, in units of 1.25 ms */
    uint16 latency;         /*!< LE Peripheral Latency, in number of Connection Events */
    uint16 timeout;         /*!< LE Link Supervision Timeout, in units of 10 ms */
    uint32 dev_class;       /*!< BR/EDR Class of Device */
} CON_MANAGER_INTERNAL_ACL_OPENED_IND_T;

/*! \brief Internal structure aggregating all information from an ACL-closed indication  */
typedef struct
{
    tp_bdaddr tpaddr;       /*!< Typed address of closed ACL */
    hci_status status;      /*!< HCI status of the disconnection */
    uint16 flags;           /*!< DM flags, meanings defined in dm_prim.h */
} CON_MANAGER_INTERNAL_ACL_CLOSED_IND_T;

/*! \brief Internal structure aggregating all information from an LE Connection Update Complete indication  */
typedef struct
{
    typed_bdaddr taddr;     /*!< Typed address of closed ACL */
    hci_status status;      /*!< HCI status of the disconnection */
    uint16 interval;        /*!< LE Connection Interval, in units of 1.25 ms */
    uint16 latency;         /*!< LE Peripheral Latency, in number of Connection Events */
    uint16 timeout;         /*!< LE Link Supervision Timeout, in units of 10 ms */
} CON_MANAGER_INTERNAL_LE_CONNECTION_UPDATE_COMPLETE_IND_T;

/*! \brief Send Write page timeout to BlueStack
    \param page_timeout The page timeout
 */
void conManagerSendWritePageTimeout(uint16 page_timeout);

/*! \brief Send Open ACL request to BlueStack
    \param tpaddr The address of the remote device
 */
void conManagerSendOpenTpAclRequest(const tp_bdaddr* tpaddr);

/*! \brief Send internally a message to open an ACL 

    \param connection The connection being created
 */
void conManagerSendOpenTpAclRequestInternally(cm_connection_t *connection);

/*! \brief Configure role switch policy.

    Currently we want to never automatically ask for a role switch nor
    refuse a request for a role switch.
 */
void ConManagerSetupRoleSwitchPolicy(void);

/*! \brief Send Close ACL request to BlueStack
    \param tpaddr The address of the remote device
    \param force Set TRUE to force close the ACL immediately regardless of
           whether there are active L2CAP connections.
    \param reason_code Set the HCI reason code that needs to sent to the other side.
 */
void conManagerSendCloseTpAclRequest(const tp_bdaddr* tpaddr, bool force, uint8 reason_code);

/*! \brief Send a Cancel Open ACL request to BlueStack
    \param tpaddr The address of the remote device
 */
void conManagerSendCancelOpenTpAclRequest(const tp_bdaddr* tpaddr);

/*! \brief Send internal message to update QoS
    \param connection The connection to send the message to
 */
void conManagerSendInternalMsgUpdateQos(cm_connection_t* connection);

/*! \brief Send internal message to set the global QoS, and update created link

    \param connection The connection to send the message to
 */
void conManagerSendInternalMsgUpdateQosDelayed(cm_connection_t* connection);

/*! \brief Send message to apply a specific QoS

    Message will be delayed if another QoS change for the same connection
    is in progress, and will be cancelled if another QoS is requested before
    this is actioned 

    \param connection Connection to update
    */
void conManagerSendInternalMsgApplyQos(cm_connection_t* connection);

/*! \brief Handle a message sent to a connection task
    \param task The task the message was sent to
    \param id The message ID
    \param id The message payload
 */
void ConManagerConnectionHandleMessage(Task task, MessageId id, Message message);

#endif
/*! @} */
